﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;
using org.alex.edss;

namespace testClient
{
    class Program
    {
        static MemoryStream ms = new MemoryStream();

        static int count = 0;

        static void Main(string[] args)
        {
            IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
            string localip = string.Empty;
            if (ips.Length > 0)
            {
                localip = ips[0].ToString();
            }
            Connector conn = new Connector(localip, 3333, 1024);
            conn.ConnectSuccessfull += new ConnectionEventHandler(conn_ConnectSuccessfull);
            conn.ConnectError += new ConnectionEventHandler(conn_ConnectError);
            conn.OnRead += new ConnectionEventHandler(conn_OnRead);
            conn.OperationError += new ConnectionEventHandler(conn_OperationError);
            conn.ReadComplete += new ConnectionEventHandler(conn_ReadComplete);
            conn.WriteComplete += new ConnectionEventHandler(conn_WriteComplete);
            conn.connect(localip, 8888);
            Console.Read();
        }

        static void conn_WriteComplete(Connector Sender, object Args)
        {
            //throw new NotImplementedException();
            count++;
            Console.WriteLine("send Ping ok! at:" + Thread.CurrentThread.ManagedThreadId);
            if (count < 2)
            {
                Sender.Read();
            }
            else
            {
                Sender.Disconnect();
            }
        }

        static void conn_ReadComplete(Connector Sender, object Args)
        {
            //throw new NotImplementedException();
            Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()) + " at:" + Thread.CurrentThread.ManagedThreadId);
            ms.SetLength(0);
            Thread.Sleep(10);
            Sender.Write(Encoding.UTF8.GetBytes("Ping......................."));
        }

        static void conn_OperationError(Connector Sender, object Args)
        {
            //throw new NotImplementedException();
            Console.WriteLine("Error " + Args.ToString() + " at:" + Thread.CurrentThread.ManagedThreadId);
        }

        static void conn_OnRead(Connector Sender, object Args)
        {
            //throw new NotImplementedException();
            byte[] data = (byte[])Args;
            ms.Write(data, 0, Sender.Offset);
        }

        static void conn_ConnectError(Connector Sender, object Args)
        {
            //throw new NotImplementedException();
            Console.WriteLine("Connect Error " + Args.ToString() + " at:" + Thread.CurrentThread.ManagedThreadId);
        }

        static void conn_ConnectSuccessfull(Connector Sender, object Args)
        {
            //throw new NotImplementedException();
            Console.WriteLine("Connect Success! at:" + Thread.CurrentThread.ManagedThreadId);
            Console.WriteLine("Send Ping........");
            Sender.Write(Encoding.UTF8.GetBytes("Ping............."));
        }
    }
}
